<?php
class UsersController extends AppController{
	var $name = 'Users';
	var $uses = array('User','Userinfo','BlogsCategory');
	var $components = array('Cookie','Captcha');
	var $autoLayout = false;
	var $autoRender = false;

	function index(){
		$this->render('/users/login');
	}
	function register(){
		// $this->autoLayout=false;
		if(!empty ($this->data)){
				$this->data['User']['regdateline']=time();
				if($this->Captcha->check($this->data['User']['captcha'])){
					$this->data['User']['password'] = md5($this->data['User']['password']);
					if($this->User->save($this->data)){
						$para=array(
							'conditions'=>array(
								"User.username"=>$this->data['User']['username'],
								"User.password"=>$this->data['User']['password']
							)
						);
						$user=$this->User->find('first',$para);
						$this->Cookie->write('User.username',$this->data['User']['username'],false,36000000);
						$this->Cookie->write('User.password',$this->data['User']['password'],false,36000000);
						// $this->Cookie->write('firstlogin','1',false); 
						$this->Session->write('User.user_id',$user['User']['id']);
						// $this->Session->write('User.nickname',$user['User']['nickname']);

						//在Userinfo表中为用户新建一条记录
						$para = array();
						$para['Userinfo']['email'] = $this->data['User']['email'];
						$para['Userinfo']['id'] = $this->Session->read('User.user_id');
						// var_dump($this->data);
						// var_dump($para);
						$this->Userinfo->save($para);
						unset($para);

						//添加博客默认分类
						$this->BlogsCategory->save(array(
							'BlogsCategory' => array(
								'user_id' => $this->Session->read('User.user_id'),
								'name' => '默认分类'
							)
						));
						$this->redirect(array('controller'=>'Blogs','action'=>'index#firstLogin'));
					}else{
						$this->Session->setFlash('注册失败,请重试');
						$this->redirect(array('controller' =>'Users','action'=>'index#register'));
					}
				}else{
					$this->Session->setFlash('验证码错误');
					$this->redirect(array('controller' =>'Users','action'=>'index#register'));
				}
		}
		// $this->render('/users/login');
	}

	function login(){
		// $this->autoLayout=false;
		if(!empty($this->data)){
			$para=array('conditions'=>array("User.username"=>$this->data['User']['username'],
											"User.password"=>md5($this->data['User']['password'])));
			if($user=$this->User->find('first',$para)){
				$userinfo = $this->Userinfo->find('first',array(
					'conditions'=>array('id'=>$user['User']['id']),
					'fields'=>array('nickname'))
				);
				$this->Session->write('User.user_id',$user['User']['id']);
				$this->Session->write('User.nickname',$userinfo['Userinfo']['nickname']);
				if($this->data['User']['checkbox']){
					$this->Cookie->write('User.username',$user['User']['username'],false,36000000);
					$this->Cookie->write('User.password',$user['User']['password'],false,36000000);
				}
				$this->Session->setFlash('登陆成功.');
				$this->redirect(array('controller'=>'Blogs','action'=>'index'));
			}else{
				$this->Session->setFlash('账号密码有误');
				$this->redirect(array('controller' =>'Users','action'=>'index#login'));
			}
		}
	}

	function logout(){
		$this->Session->destroy();
		$this->Cookie->destroy();
		$this->redirect(array('controller'=>'Users','action'=>'index#login'));
	}

	function captcha(){
		$this->Captcha->render();
	}
}
?>
